from app.extensions import SessionLocal
from app.models.Clas import Clas
from app.models.Student import Student

class StudentRepository():
    def __init__(self, db: SessionLocal):
        self.db = db

    def get_by_id(self, student_id):
        """
        根据ID查询学生
        :param student_id: 学生ID
        :return: 学生对象或None
        """
        try:
            return self.db.query(Student).filter(Student.id == student_id).first()
        except Exception as e:
            print(f'查询错误: {e}')
            return None

    def get_by_keyword(self, keyword):
        """
        根据关键字查询数据
        :param keyword: 关键字
        :return: 如果查询成功返回数据，查询失败返回空列表
        """
        try:
            data = self.db.query(Student).filter(Student.name.like(f'%{keyword}%')).all()
            return data
        except Exception as e:
            print(f'查询错误: {e}')
            return []

    def get_by_name(self, name):
        """
        根据姓名查询学生
        :param name: 学生姓名
        :return: 学生对象或None
        """
        try:
            return self.db.query(Student).filter(Student.name == name).first()
        except Exception as e:
            print(f'查询错误: {e}')
            return None

    def get_student_by_id(self, stu_id):
        '''
        通过ID获取学生信息
        :param stu_id:
        :return:
        '''
        try:
    #         假设self.db是一个SQLAlchemy会话或类似对象
            return self.db.query(Student).filter_by(id=stu_id).first()
        except Exception as e:
            print(f"学生id错误:{e}")
            return None

    def add_student(self, clas_id, name, gender):
        try:
            stu = Student(name=name, gender=gender, clas_id=clas_id)
            self.db.add(stu)
            self.db.commit()
            return True
        except Exception as e:
            print(e)
            return False

    # def update_student(self, student_id, clas_id, name, gender):
        # try:
        #     student = self.get_by_id(student_id)
        #     if not student:
        #         return False
        #
        #     student.clas_id = clas_id
        #     student.name = name
        #     student.gender = gender
        #     self.db.commit()
        #     self.db.refresh(student)
        #     return True
        # except Exception as e:
        #     print(f'更新错误: {e}')
        #     self.db.rollback()
        #     return False
    def update_student(self, stu_id, new_name, new_gender, new_clas_id):
        try:
            stu = self.get_student_by_id(stu_id)
            stu.name = new_name if new_name != "" else stu.name
            stu.gender = new_gender if new_gender != "" else stu.gender
            stu.clas_id = int(new_clas_id) if new_name != "" else stu.clas_id
            self.db.commit()
            return  True
        except:
            return False

    def delete_student(self, student_id):
        try:
            student = self.get_by_id(student_id)
            if not student:
                return False

            self.db.delete(student)
            self.db.commit()
            return True
        except Exception as e:
            print(f'删除错误: {e}')
            self.db.rollback()
            return False